package cn.dbwork.yacht.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.dbwork.yacht.dao.IUserDao;
import cn.dbwork.yacht.dao.impl.UserDao;
import cn.dbwork.yacht.domain.User;
import cn.dbwork.yacht.service.IUserService;
import cn.dbwork.yacht.service.impl.UserService;

@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		IUserService userService=new UserService();
		
		String account = request.getParameter("account");
		String password = request.getParameter("password");
		String remember = request.getParameter("remember");
	
		System.out.println("account:" + account + "password:" + password);
		User user = userService.findStuAndPass(account, password);
		System.out.println(user);
		if(user == null) {
			//用户输入的账号或密码错误，跳转至登录页面，并给予提示信息
			request.setAttribute("error", "您输入的学号或密码错误！");
			System.out.println("===========错误跳转================");
			//return "forward:/index";
			request.getRequestDispatcher("index.jsp").forward(request, response);
			//response.sendRedirect("index.jsp");
		}else {
			//用户输入的学号密码正确，登陆成功，跳转至主页面
			//保存在session中的数据,默认30分钟内有效
			request.getSession().setAttribute("session_user", user);
			request.getSession().setAttribute("LoginInfo","登录成功");
			System.out.println("===========跳转主页面================");
			
			System.out.println(request.getSession().getAttribute("session_user"));
			//return "main";
			request.getRequestDispatcher("WEB-INF/pages/main.jsp").forward(request, response);
			//response.sendRedirect("WEB-INF/pages/main.jsp");
			
			
		}
	}

}
